<?php

/**
 * @Author: Mr.Mao
 * @Date:   2021-05-24 14:28:40
 * @Last Modified by:   Mr.Mao
 * @Last Modified time: 2021-05-25 15:28:26
 */
namespace app\weixin\model;
use app\BaseModel;
use think\facade\Cache;
use think\Db;
//*------------------------------------------------------ */
//-- 自定义交易组件
/*------------------------------------------------------ */
class WeixinVideoBrandModel extends BaseModel
{
	protected $table = 'weixin_video_brand';
	public  $pk = 'id';
	protected $mkey = 'weixin_video_brand_';
	/**
	 * [info 品牌详情]
	 * @param  integer $id [品牌id]
	 * @return [type]      [description]
	 */
	public function info($id = 0)
	{
		$info = $this->find($id);
		if (empty($info)) {
			return '品牌部存在';
		}

		$info = $info->toArray();

    	if ($info['status'] == 0) {
    		$data['audit_id'] = $info['audit_id'];
    		$res = $this->auditResult($data);
    		if ($res['errcode'] > 0) {
    			return $res['errmsg'];
    		}
    		if ($info['status'] != $res['data']['status']) {
    			$upData = [];
	    		$upData['status'] = $res['data']['status'];
	    		$upData['reject_reason'] = $res['data']['reject_reason'];
	    		$upData['update_time'] = time();
	    		$ress = $this->where('id',$info['id'])->update($upData);
	    		if ($ress < 1) {
	    			return '修改品牌状态失败.';
	    		}
    		}
    		$info['status'] = $res['data']['status'];
            $info['reject_reason'] = $res['data']['reject_reason'];
    	}

    	$info['brand_info'] = json_decode($info['brand_info'],true);

    	return $info;
    	
	}

	/**
     *  [getRows 获取类目列表]
     * @param  array  $where [搜索条件]
     * @return [type]        [description]
     */
    public function getRows($where = [])
    {
        $rows = Cache::get($this->mkey);
        if (empty($rows)) {
            $rows = $this->where($where)->select()->toArray();
            Cache::set($this->mkey,$rows);
        }
        return $rows;
    }

	/**
	 * [getBrandList 获取品牌列表]
	 * @return [type] [description]
	 */
	public function getBrandList()
	{
		$ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/account/get_brand_list?access_token=$ACCESS_TOKEN";
        return $this->curlBrand($url);
	}
	/**
	 * [auditBrand 品牌申请]
	 * @param  string $param [申请参数]
	 * @return [type]        [description]
	 */
	public function auditBrand($param='')
	{
		$ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/audit/audit_brand?access_token=$ACCESS_TOKEN";
        return $this->curlBrand($url ,$param);
	}

	/**
	 * [getShopApply 自定义版交易组件查询审核结果]
	 * @param  array  $param [请求参数]
	 * @return array         [请求返回]
	 */
    function auditResult($param = array()){

        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/audit/result?access_token=$ACCESS_TOKEN";
        return $this->curlBrand($url,$param);
    }
	

	/**
     * [curlCategory curl]
     * @param  string $url   [description]
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function curlBrand($url='',$param = array())
    {
    	$data = json_encode($param);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        return json_decode($result,true);
    }
}